/**
 * May 6, 2014 3:22:44 PM
 */
package elitech.vietnam.copyweatherfashion.adapters;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.copyweatherfashion.LoginActivity;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment.ConfirmDialogClick;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment.Type;
import elitech.vietnam.copyweatherfashion.dialog.WarningDialogFragment;
import elitech.vietnam.copyweatherfashion.entities.Post;
import elitech.vietnam.copyweatherfashion.fragments.MemberPostFragment;
import elitech.vietnam.copyweatherfashion.fragments.StorePostFragment;
import elitech.vietnam.copyweatherfashion.widget.ButtonLike;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 *
 */
public class StyleGridAdapter extends ArrayAdapter<Post> {
	
	MainActivity mActivity;
	
	boolean isHeartMode = false;
	int mItemHeight;

	public StyleGridAdapter(Context context, int resource, List<Post> objects) {
		super(context, resource, objects);
		mActivity = (MainActivity) context;
		mItemHeight = (int) (mActivity.mConfig.getScreenHeigh()/2.5);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;
		
		if (row == null) {
			row = LayoutInflater.from(getContext()).inflate(R.layout.style_item, parent, false);
			holder = new ViewHolder();
			
			holder.mImage = (ImageView) row.findViewById(R.id.styleitem_imgPost);
			holder.mBtnLike = (ButtonLike) row.findViewById(R.id.styleitem_btnLike);

			LayoutParams params = holder.mImage.getLayoutParams();
			params.height = mItemHeight;
			holder.mImage.setLayoutParams(params);
			
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();
		
		final Post mPost = getItem(position);
		UrlImageViewHelper.setUrlDrawable(holder.mImage, Const.SERVER_IMAGE_THUMB_URL + mPost.Image);
		holder.mBtnLike.setData(mPost.Liked(), mPost.Likes);
		
		holder.mImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MainActivity mActivity = (MainActivity) getContext();
				if (mPost.Type == 1) {
					mActivity.mShopPost = mPost;
					mActivity.getCurrentBase().replaceFragment(new StorePostFragment(), true);
				} else {
					mActivity.mStylePost = mPost;
					mActivity.getCurrentBase().replaceFragment(new MemberPostFragment(), true);
				}
			}
		});
		
		holder.mClick = new ConfirmDialogClick() {
			MainActivity mActivity = (MainActivity) getContext();
			@Override
			public void yesClick() {
				new AsyncTask<Integer, Integer, Integer>() {
					@Override
					protected void onPreExecute() {
						holder.mBtnLike.setEnabled(false);
					}
					@Override
					protected Integer doInBackground(Integer... params) {
						try {
							return new Service().newLike(mActivity.mUser.Id, mPost.Id, mPost.Liked() ? -1 : 1);
						} catch (IOException | XmlPullParserException e) { e.printStackTrace();
							return null;
						}
					}
					@Override
					protected void onPostExecute(Integer result) {
						holder.mBtnLike.setEnabled(true);
						if (result == null || result == -1) {
							new WarningDialogFragment.Builder()
							.setMessage(mActivity.getString(R.string.cannot_connect_to_server))
							.build().show(mActivity.getSupportFragmentManager());
						} else {
							mPost.Liked(!mPost.Liked());
							mPost.Likes = result;
							holder.mBtnLike.setData(mPost.Liked(), mPost.Likes);
							if (isHeartMode) {
								remove(mPost);
								notifyDataSetChanged();
							}
						}
					}
				}.execute();
			}
			@Override
			public void noClick() {
			}
		};
		
		holder.mBtnLike.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View v) {
				if (mActivity.mUser == null) {
					Intent intent = new Intent(mActivity, LoginActivity.class);
					mActivity.startActivityForResult(intent, MainActivity.REQUEST_LOGIN);
				} else {
					final String ms = (mPost.Liked())
							? mActivity.getString(R.string.do_you_want_to_unlike_this_post_)
							: mActivity.getString(R.string.do_you_want_to_like_this_post_);
					new ConfirmDialogFragment.Builder()
						.setMessage(ms)
						.setTargetFragment(holder)
						.setType(Type.FRAGMENT)
						.build().show(mActivity.getSupportFragmentManager());
				}
			}
		});
		
		return row;
	}
	
	public void setHeartMode(boolean mode) {
		isHeartMode = mode;
	}
	
	public static class ViewHolder extends Fragment implements ConfirmDialogClick {
		ImageView mImage;
		ButtonLike mBtnLike;
		ConfirmDialogClick mClick;
		@Override
		public void yesClick() {
			if (mClick != null)
				mClick.yesClick();
		}
		@Override
		public void noClick() {
		}
	}
}
